package com.cm;

import com.cm.bi.History;
import com.cm.bi.Reporter;
import com.cm.cms.EmailNotifier;
import com.cm.command.Command;
import com.cm.command.CommandGenerator;
import com.cm.drinks.Drink;
import com.cm.order.Order;

public class CoffeeMachine {
    private DrinkMaker maker;
    private Reporter reporter;
    private History history = new History();
    private EmailNotifier notifier;
    private BeverageQuantityChecker checker;

    public CoffeeMachine(DrinkMaker maker, Reporter reporter, EmailNotifier notifier, BeverageQuantityChecker checker) {
        this.maker = maker;
        this.reporter = reporter;
        this.notifier = notifier;
        this.checker = checker;
    }

    /**
     * 收到订单请求
     * @param order 订单
     * @param payment 实际付款金额，注意：未做 charge
     */
    public void order(Order order, double payment){
        if (payment <= 0){
            throw new IllegalArgumentException();
        }
        // 1. 先检查库存
        Command command;
        Drink drink = order.getDrink();
        if (checker.isEmpty(drink)){
            // 通知公司缺货
            notifier.notifyMissingDrink(drink);
            // 给客户发送通知
            command = CommandGenerator.message("Shortage of drink: " + drink.getName());
            maker.execute(command.command());
            return;
        }
        // 2. 生成 com.cm.command
        command = CommandGenerator.generate(order, payment);
        // 3. 调用 maker 去生成订单项
        maker.execute(command.command());
        // 4. 数据统计
        history.statistic(order);
    }

    public void summary() {
        reporter.report(history);
    }
}
